<?php

class Admin_ProductitemController extends Zend_Controller_Action
{
	private $_params;
	private $_auth;	
    public function init(){
        $this->_params      = $this->_request->getParams();        
        $this->_auth         = new My_Auth();
        $this->view->auth    = $this->_auth;        
        $this->_auth        = $this->_auth->checkAuthentication();
        $this->view->auth   = $this->_auth;
    }
    public function indexAction(){
        $modelItem  = new Admin_Model_Productitem();                
        $itemList   = $modelItem->getAllData()->toArray();                        
        $this->view->itemList = $itemList;        
    }    
    public function deleteAction(){
        $proId = $this->_getParam('id', 0);
        $modelPro = new Admin_Model_Productitem();
        $proCheck = $modelPro->getDataById($proId);
        if($proCheck){
            $this->view->proCheck = $proCheck;
            if($this->getRequest()->isPost()){
                $del = $this->getRequest()->getPost('del');
                if($del == 'Có'){
                    $proId = $this->getRequest()->getPost('id');
                    $modelPro->deleteData($proId);               
                }
                $this->_helper->redirector('index', 'productitem', 'admin');
            }
        }
    }
    public function addAction(){
        $form = new Admin_Form_Productitem();
        $form->submit->setLabel('Thêm mới');
        $this->view->form = $form;                        
        if ($this->getRequest()->isPost()) {            
            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData)) {
                //pr($formData);                
                $addData['catId']    = $form->getValue('catId');
                $addData['name']    = $form->getValue('name');
                $addData['shortName']    = $this->_helper->setup($addData['name']);;
                $addData['ord']    = $form->getValue('ord');
                $addData['display']    = $form->getValue('display');                
                
                $modelItem = new Admin_Model_Productitem();				
				$addId = $modelItem->addData($addData);
                if($addId){                    
                    $this->_helper->redirector('index', 'productitem', 'admin');
                }else{
                    $this->view->mess = "Đã xảy ra lỗi; Vui lòng thử lại sau!!";
                    $this->view->flag = "alert";  
                }
                	
            }else{
                $form->populate($formData);
            }
        }
    }
    public function editAction(){
		$form = new Admin_Form_Productitem();
        $modelPro = new Admin_Model_Productitem();
		$form->submit->setLabel('Lưu thay đổi');
		$this->view->form = $form;
        $id = (int)($this->_getParam('id', 0));
        $proCheck = $modelPro->getDataById($id);
		if($this->getRequest()->isPost()){
			$formData = $this->getRequest()->getPost();
			if($form->isValid($formData)){
				$dataUpdate['id'] = (int)($formData['id']);
				$dataUpdate['name'] = ($formData['name']);
                $dataUpdate['shortName']    = $this->_helper->setup($dataUpdate['name']);;
                $dataUpdate['ord'] = (int)($formData['ord']);
				$dataUpdate['display'] = ($formData['display']);
                $dataUpdate['catId'] = ($formData['catId']);
                if($proCheck){  
				    $modelPro->updateData($id, $dataUpdate);
                    $this->_helper->redirector('index', 'productitem', 'admin'); 
                }else{
                    $this->view->mess ="Không tồn tại danh mục này!!!";
                    $this->view->flag = "alert";
    				$form->populate($formData);
    			}

			}else{
				$form->populate($formData);
			}
		}else{
		    $form->populate($proCheck->toArray());
		}
    }
}